package com.lej.my.information

import com.lej.core.base.BaseModel
import com.lej.core.base.BaseViewModel
import com.lej.core.common.PCacheInfo
import com.lej.core.utils.extention.toast
import com.lej.dataservice.club.http.base.getBody
import com.lej.dataservice.club.http.checkData
import com.lej.my.http.myApiService
import kotlinx.coroutines.flow.MutableStateFlow

/**
 *  author: cjq
 *  date  : 2022/7/27 0027
 *  desc  :
 */
class MyInformationVM : BaseViewModel<BaseModel>() {
    var isNormalLayout = MutableStateFlow(true)
    var name = MutableStateFlow("")

    suspend fun updateUserName(): Boolean {
        val nameValue = name.value
        if (nameValue.isEmpty()) {
            "请输入用户名".toast()
            return false
        }
        if (nameValue == PCacheInfo.nick) {
            return false
        }
        showDialog()
        kotlin.runCatching {
            myApiService.updateInfo(getBody("nick" to nameValue)).checkData()
            PCacheInfo.nick = nameValue
            dismissDialog()
            return true
        }.onFailure {
            it.message.toast()
        }
        dismissDialog()
        return false
    }

}